#include "junix.h"
#include "page_type.h"

namespace jsearch
{
	
 char * page_type_info[]=
{
	"ASCII",
	"HTML"
};

int page_type(const char *path) 
{
	if (!path)
		return PAGE_TYPE_ERROR;

	const int size=2048;
	char buf[size];

	strcpy(buf,"file -b ");
	strcat(buf,path);

	FILE *fp;
	if ((fp=popen(buf,"r")) == NULL) {
		perror("file cmd not founded");
		exit(1);
	}

	if (fgets(buf, size, fp)==NULL)
		err_quit("fgets error");

	int ret;
	if ((ret=pclose(fp)))
		err_sys("pclose");


	const int sz = sizeof(page_type_info)/sizeof(char*);
	for (int i=0; i<sz; i++) {
		if (strstr(buf, page_type_info[i]) != NULL)
			return i;
	}

	return PAGE_TYPE_OTHER ;
}

}//end namespace
